import os
import time
import hmac
import hashlib
import base64
import urllib.parse

# timestamp = str(round(time.time() * 1000))
# secret = 'SEC1096f863308ee876da555b6ad66ade1c41bdcd6a4a1b52b8861e523f7cecc270'
# secret_enc = secret.encode('utf-8')
# string_to_sign = '{}\n{}'.format(timestamp, secret)
# string_to_sign_enc = string_to_sign.encode('utf-8')
# hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
# sign = urllib.parse.quote(base64.b64encode(hmac_code))
# # print(timestamp)
# # print(sign)
#
#
# import requests,json   #导入依赖库
#
# # url = os.path.join(os.path.join(BASE_DIR,'static/upload'), '', 'wallhaven-39xv99.jpg')
# def send_ding():
#     headers={'Content-Type': 'application/json'}   #定义数据类型
#     # https://oapi.dingtalk.com/robot/send?access_token=223bc07b2d8fad1333d4952b9277b91775ad1f66fb558898d68135d45bf9317f
#     webhook = 'https://oapi.dingtalk.com/robot/send?access_token=223bc07b2d8fad1333d4952b9277b91775ad1f66fb558898d68135d45bf9317f&timestamp='+timestamp+"&sign="+sign
#     #定义要发送的数据
#     #"at": {"atMobiles": "['"+ mobile + "']"
#     # os.path.join(UPLOAD_ROOT, '', 'wallhaven-39xv99.jpg'
#     data = {
#     "actionCard": {
#         "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
#         "text": "![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n ### 乔布斯 20 年前想打造的苹果咖啡厅\nApple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
#         "btnOrientation": "0",
#         "singleTitle" : "阅读全文",
#         "singleURL" : "https://www.dingtalk.com/"
#     },
#     "msgtype": "actionCard"
# }
#     res = requests.post(webhook, data=json.dumps(data), headers=headers)   #发送post请求
#
#     print(res.text)
# while 1:
#     send_ding()


# from __future__ import absolute_import, unicode_literals

import datetime
import time
import requests
from optionaldict import optionaldict

# token: 3b49ddeff1e63266b2528a3ec41bf127
# userid： 156711604927817570
# unionid: 'GrEzjClZhUR1klTodiS2hFwiEiE'

# https://oa.dingtalk.com

# url = 'https://oapi.dingtalk.com/topapi/workrecord/add?access_token='
#
# data = {
#     'userid': 'manager7695',
#     'create_time': 1496678400000,
#     'title': "1222222222",
#     'url': 'https://oa.dingtalk.com',
#     'formltemList': {"title": "321111", "conent": "hhhhhhhhhh"}
# }
# res = requests.post(url=url, data=data)
# print(res.text)



# res = requests.get(url='https://oapi.dingtalk.com/user/get_by_mobile?access_token=ywbj0kvamrvYgx4_JaLC2-sKJj07UgvtDIHhCDNDV4aJFfcHrvpE1sglVChsehzU')
#
# print(res.text)


# 钉钉待办sdk
from dingtalk import *
corpid = 'dingbbd8529c9b3f555ea1320dcb25e91351'
appkey = 'ding7jeha9fw7paqifm0'
AppSecret = 'ywbj0kvamrvYgx4_JaLC2-sKJj07UgvtDIHhCDNDV4aJFfcHrvpE1sglVChsehzU'

req = AppKeyClient(corpid, appkey , AppSecret)

req.workrecord.add(userid='manager483', create_time=int((time.time()))*1000000000, title="1111", url="baidu.com", form_item_dict={"title": "321", "content": "112233"})


import requests
import json
import time

AppKey='ding7jeha9fw7paqifm0'
AppSecret='ywbj0kvamrvYgx4_JaLC2-sKJj07UgvtDIHhCDNDV4aJFfcHrvpE1sglVChsehzU'

api_url='https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s' % (AppKey,AppSecret)

#获取钉钉access_token
def get_token():
    res = requests.get(api_url).text
    # print(res)
    if json.loads(res).get('errcode') == 0:
        token = json.loads(res).get('access_token')
        print(token)
        return token

time= int(time.time())*1000000
print(time)


#发起代办
def addworkrecord():
    addworkrecord_url = 'https://oapi.dingtalk.com/topapi/workrecord/add?access_token=%s' % (get_token())
    body = {
        #unix时间
        "create_time": time,
        "formItemList": {
            #内容标题
            "title": "asasgda",
            #内容
            "content": "sdfsfgs"
        },
        #待办标题
        "title": "234",
        #用户id
        "userid": "manager483",
        "url": "baidu.com",
        #发起人id
        "originator_user_id": "196325036721853098",
        #代办来源名称
        "source_name": "adg"
    }
    res = requests.post(addworkrecord_url, json.dumps(body))
    print(res.text)

addworkrecord()




